//
//  SubjectPulldownView.m
//  jzyParents
//
//  Created by 任辰东 on 2022/8/20.
//

#import "SubjectPulldownView.h"
#import "UIButton+BackgroundColor.h"
#import "UIButton+LXMImagePosition.h"

@interface SubjectCell : UITableViewCell

@property (strong, nonatomic) UILabel *infoLabel;
@end

@implementation SubjectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupViews];
        [self setupLayout];
    }
    return self;
}

-(void)configViewWithModel:(SubjectModel *)model
{
    self.infoLabel.text = model.name;
    if (model.isSelected) {
        self.infoLabel.textColor = UIColorFromHex(kColorMain);
    }else{
        self.infoLabel.textColor = UIColorFromHex(kColorBlack);
    }
}

- (void)setupViews
{
    [self.contentView addBottomLineWithEdge:UIEdgeInsetsMake(0, 15, 0, 15)];
    [self.contentView addSubview:self.infoLabel];
}

-(void)setupLayout
{
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).offset(kPaddingTop);
        make.bottom.equalTo(self.contentView).offset(kPaddingBottom);
        make.left.equalTo(self.contentView).offset(kPaddingLeft);
        make.right.equalTo(self.contentView).offset(kPaddingRight);
        
    }];
}

#pragma mark -- lazyload

-(UILabel *)infoLabel
{
    if (!_infoLabel) {
        _infoLabel = [UILabel new];
        _infoLabel.textColor = UIColorFromHex(kColorBlack);
        _infoLabel.textAlignment = NSTextAlignmentCenter;
        _infoLabel.font = UIFontSize(14);
    }
    return _infoLabel;
}


@end


@interface SubjectPulldownView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *wrapView;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, assign) BOOL isCompleteAnimation;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (strong, nonatomic) UITableView *myTabelView;

@end

@implementation SubjectPulldownView

-(instancetype)init
{
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        [self addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self getData];
        [self setupViews];
        [self setupLayout];
        [self.myTabelView reloadData];
    }
    return self;
}

-(void)getData
{
    self.dataSourceArray = [NSMutableArray array];
    
    SubjectModel *model = [SubjectModel new];
    model.name = @"全部";
    model.isSelected = YES;
    model.index = @"0";
    
    SubjectModel *model1 = [SubjectModel new];
    model1.name = @"联考";
    model1.isSelected = NO;
    model1.index = @"1";
    
    SubjectModel *model2 = [SubjectModel new];
    model2.name = @"作业";
    model2.isSelected = NO;
    model2.index = @"4";
    
    SubjectModel *model3 = [SubjectModel new];
    model3.name = @"练习";
    model3.isSelected = NO;
    model3.index = @"3";
    
    SubjectModel *model4 = [SubjectModel new];
    model4.name = @"考试";
    model4.isSelected = NO;
    model4.index = @"2";
    [self.dataSourceArray addObject:model];
    [self.dataSourceArray addObject:model1];
    [self.dataSourceArray addObject:model2];
    [self.dataSourceArray addObject:model3];
    [self.dataSourceArray addObject:model4];
    
}

- (void)setupViews
{
    [self addSubview:self.wrapView];
    [self.wrapView addSubview:self.myTabelView];
}

- (void)setupLayout
{
    
    [self.wrapView mas_makeConstraints:^(MASConstraintMaker *make) {
       
//        make.bottom.equalTo(self.mas_top).offset(0);
//        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(0);
        make.left.right.equalTo(self);
    }];
    
    [self.myTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.equalTo(self.wrapView);
        make.height.mas_equalTo(50 * self.dataSourceArray.count);
        make.bottom.equalTo(self.wrapView).offset(0);
        
    }];
}

#pragma mark -- private

- (void)show
{
    [kKeyWindow addSubview:self];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(kKeyWindow);
        make.top.equalTo(kKeyWindow).offset(kNavitionBarHeight);
        make.left.bottom.right.equalTo(kKeyWindow);
    }];
    
    // 提交固定layout
    [self layoutIfNeeded];
    
    WeakSelf
    [self.wrapView mas_remakeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.top.equalTo(self).offset(0);
        make.left.right.equalTo(self);
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        StrongSelf
        self.isCompleteAnimation = NO;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        StrongSelf
        self.isCompleteAnimation = YES;
    }];
}

- (void)dismiss
{
    if (!self.isCompleteAnimation) {
        return;
    }
    if (self.dismissBlock) {
        self.dismissBlock();
    }
    [self removeFromSuperview];
    
    [self.wrapView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
//        make.bottom.equalTo(self.mas_top).offset(0);
//        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(0);
        make.left.right.equalTo(self);
    }];
    
    [self.myTabelView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.equalTo(self.wrapView);
        make.height.mas_equalTo(50 * self.dataSourceArray.count);
        make.bottom.equalTo(self.wrapView).offset(0);
        
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SubjectCell class])];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row < self.dataSourceArray.count) {
        SubjectModel *model = self.dataSourceArray[indexPath.row];
        [cell configViewWithModel:model];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SubjectModel *model;
    if (indexPath.row < self.dataSourceArray.count) {
        model = self.dataSourceArray[indexPath.row];
    }
    
    for (SubjectModel *smodel in self.dataSourceArray) {
        smodel.isSelected = NO;
    }
    
    model.isSelected = YES;
    [self.myTabelView reloadData];
    
    if (self.ScreenBlock) {
        self.ScreenBlock(model);
    }
    [self dismiss];
}

#pragma mark -- lazyload

-(UIView *)wrapView
{
    if (!_wrapView) {
        _wrapView = [UIView new];
        _wrapView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _wrapView;
}

- (UITableView *)myTabelView{
    if (!_myTabelView) {
        _myTabelView = [[UITableView alloc]initWithFrame:CGRectZero];
        _myTabelView.delegate = self;
        _myTabelView.dataSource = self;
        _myTabelView.backgroundColor = [UIColor clearColor];
        _myTabelView.scrollEnabled = NO;
        _myTabelView.separatorColor = [UIColor clearColor];
        [_myTabelView registerClass:[SubjectCell class] forCellReuseIdentifier:NSStringFromClass([SubjectCell class])];
    }
    return _myTabelView;
}

@end
